package hamsterteam.hop.controller;

import hamsterteam.hop.ejb.EtudiantEJB;
import hamsterteam.hop.model.Etudiant;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@SessionScoped
public class EtudiantController {
    private Etudiant selected;

    @EJB
    private EtudiantEJB etudiantEJB;

    private HtmlDataTable dataTable;

    private ListDataModel etudiantList;

    private String recherche;

    private void updateEtudiantList() {
        etudiantList = new ListDataModel(etudiantEJB.findAll());
    }
    
    public String doCancel() {
        updateEtudiantList();
        return "listEtudiant.xhtml";
    }

    public String doEdit() {
        return "editEtudiant.xhtml";
    }
    
    public String doCancelEdit() {
        return "viewEtudiant.xhtml";
    }
    
    public String doSelect() {
        updateEtudiantList();   
        HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        long selectedId = Long.parseLong(req.getParameter("selected"));
        if (selectedId > 0)
        {
            selected = etudiantEJB.findId(selectedId);
        }
        return "viewEtudiant.xhtml";
    }

    public String doSave() {
        etudiantEJB.update(selected);
        return "viewEtudiant.xhtml";
    }

    public ListDataModel getEtudiantList() {
        updateEtudiantList();
        return etudiantList;
    }

    public void setEtudiantList(ListDataModel projetList) {
        this.etudiantList = projetList;
    }

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public String getRecherche() {
        return recherche;
    }

    public void setRecherche(String recherche) {
        this.recherche = recherche;
    }

    public Etudiant getSelected() {
        return selected;
    }

    public void setSelected(Etudiant selected) {
        this.selected = selected;
    }

}